package com.first.computerstore.service;

import com.first.computerstore.dao.AddressMapper;
import com.first.computerstore.dao.DistrictMapper;
import com.first.computerstore.entity.Address;
import com.first.computerstore.utils.AssertUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * service层
 */
@Service
public class AddressService {

    @Resource
    private AddressMapper addressMapper;


    /**
     * 根据用户id查询用户所有地址
     * @param uid
     * @return
     */
    public List<Address> queryAddressListByUid(Integer uid) {
        //校验参数
        AssertUtil.isTrue(null==uid,"用户id不能为空！");
        List<Address> addressList = addressMapper.queryAddressListByUid(uid);

        return addressList; }


    @Resource
    private DistrictMapper districtMapper;
    /**
     * 新增地址
     * @param uid
     * @param address
     * @return
     */
    public void addAddress(Integer uid, Address address) {
        //先去查询当前uid下有的地址
        List<Address> oldAddressList =  addressMapper.queryAddressByUid(uid);
        //根据选择地址的下拉框中的code获取对应的name，地区省份以及城市
        address.setProvinceName(districtMapper.queryDistrictByCode(address.getProvinceCode()));
        address.setCityName(districtMapper.queryDistrictByCode(address.getCityCode()));
        address.setAreaName(districtMapper.queryDistrictByCode(address.getAreaCode()));
        address.setUid(uid);
        AssertUtil.isTrue(oldAddressList.equals(address),"地址重复");
        AssertUtil.isTrue(addressMapper.addnewAddress(address)<1,"添加地址失败");
        //判断总地址数，如果超过6条就不可再次新增
        AssertUtil.isTrue(addressMapper.queryAddressCount(uid)>6,"地址到达上限");
        //当查询记录为0，则将此地址设置为用户默认的地址
        if (addressMapper.queryAddressCount(uid) == 0){
            address.setIsDefault(1);
        }
    }

    /**
     * 地址显示
     * @param uid
     * @return
     */
    public List<Address> ShowAddress(Integer uid) {
        return addressMapper.queryAddressByUid(uid);
    }

    /**
     * 设置地址默认值
     * @param uid
     * @param aid
     */
    public void setAddressStatus(Integer uid, Integer aid) {
        //设置默认地址需要先将所有默认状态都变成0
        //所以先单独写一个更改状态的方法，根据uid先去设置
        AssertUtil.isTrue(addressMapper.removeAddressStatus(uid)<1,"设置默认地址失败");
        //现在所有的默认地址状态都为0，设置要显示的默认地址的状态为1
        AssertUtil.isTrue(addressMapper.setAddressStatus(uid,aid)<1,"设置默认地址失败");
    }


    /**
     * 点击修改的数据回显
     * @param aid
     * @return
     */
    public Address queryOneAddress(String aid) {
        Address address = addressMapper.queryOneAddress(aid);
        return address;
    }

    /**
     * 更新地址操作
     * @param address
     */
    public void updateAddress(Address address) {
        if (address.getIsDefault() == null){
            address.setIsDelete(0);
        }
        if(address.getIsDelete() == null){
            address.setIsDelete(0);
        }
        //根据选择地址的下拉框中的code获取对应的name，地区省份以及城市
        address.setProvinceName(districtMapper.queryDistrictByCode(address.getProvinceCode()));
        address.setCityName(districtMapper.queryDistrictByCode(address.getCityCode()));
        address.setAreaName(districtMapper.queryDistrictByCode(address.getAreaCode()));
        address.setModifiedTime(new Date());
        AssertUtil.isTrue(addressMapper.updateAddress(address)<1,"更新失败");
    }

    /**
     * 删除地址，走的逻辑删除
     * @param aid
     */
    public void deleteAddress(String aid) {
        AssertUtil.isTrue(addressMapper.deleteAddress(aid)<1,"地址删除失败");
    }
}
